package com.zhangli.mytoolkit;

import android.content.Intent;
import android.util.Log;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import com.zhangli.broadcastreceiver.ServiceForBroadcast;
import com.zhangli.config.Config;


import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button bn_start,bn_stop,bn_save;
    EditText ed_email_from,ed_email_to,ed_username,ed_password,ed_check_time_gap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("Broadcast","create...");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bn_start = (Button)findViewById(R.id.bn_start_id);
        bn_stop = (Button)findViewById(R.id.bn_stop_id);

        bn_start.setEnabled(true);
        bn_stop.setEnabled(true);

        ed_email_from = (EditText)findViewById(R.id.EmailFrom);
        ed_email_to = (EditText)findViewById(R.id.EmailTo);
        ed_username = (EditText)findViewById(R.id.Username);
        ed_password = (EditText)findViewById(R.id.Password);
        ed_check_time_gap = (EditText)findViewById(R.id.check_time);
        Config p = Config.getInstance();
        ed_email_from.setText(p.readString("EmailFrom",""));
        ed_email_to.setText(p.readString("EmailTo",""));
        ed_username.setText(p.readString("Username",""));
        ed_password.setText(p.readPassword("Password"));
        ed_check_time_gap.setText(p.readString("DefaultCheckTimeGap",""));
    }

    public void start(View view){
        Intent service = new Intent(view.getContext(),ServiceForBroadcast.class);
        view.getContext().startService(service);
    }

    public void stop(View view){
        Intent i = new Intent(this, MyWebView.class);
        startActivity(i);
    }

    public void save(View view){
        Config p = Config.getInstance();
        p.writeString("EmailFrom",ed_email_from.getText().toString());
        p.writeString("EmailTo",ed_email_to.getText().toString());
        p.writeString("Username",ed_username.getText().toString());
        p.writePassword("Password",ed_password.getText().toString());
        p.writeString("DefaultCheckTimeGap",ed_check_time_gap.getText().toString());
        p.commit();
        Toast.makeText(view.getContext(), "Saved", Toast.LENGTH_SHORT).show();
    }
}



